package com.fs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.pojo.*;
import com.fs.service.*;
import com.fs.util.ResponseDataUtil;
import com.fs.vo.Condition;
import com.fs.vo.ResponseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hyy
 * @version 1.0
 * @description: 销售订单控制层
 * @date 2024/10/15 16:37
 */
@RestController
@RequestMapping("/sales/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private Customerservice customerservice;

    @Autowired
    private InventoryToService inventoryToService;
    /**
     * 采购订单搜索查询
     * @param condition
     * @return
     */
    @PostMapping("/query")
    private ResponseData<PageInfo<Order>> queryAll(@RequestBody Condition<Order> condition){
        PageHelper.startPage(condition.getPage(),condition.getCount());
        List querylist = orderService.querylist(condition);
        return querylist!=null&&!querylist.isEmpty()? ResponseDataUtil.buildOk(new PageInfo<Order>(querylist)): ResponseDataUtil.buildFail(querylist);
    }

    @PostMapping("/queryAll")
    private ResponseData<PageInfo<Order>> queryAllDetails(@RequestBody Condition<Order> condition){
//        PageHelper.startPage(condition.getPage(),condition.getCount());
        List querylist = orderService.querylistall(condition);
        return querylist!=null&&!querylist.isEmpty()? ResponseDataUtil.buildOk(querylist): ResponseDataUtil.buildFail(querylist);
    }

    /**
     * 通过id获取order订单信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    private  ResponseData<Order> queryOrderById(@PathVariable Integer id){
        Order order = orderService.getOrderById(id);
        return order!=null?ResponseDataUtil.buildOk(210,"查询成功",order):ResponseDataUtil.buildFail(null);
    }

    /**
     * 删除订单信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    private  ResponseData<Order> delOrderById(@PathVariable Integer id){
        Boolean order = orderService.delOrder(id);
        return order?ResponseDataUtil.buildOk(204,"删除成功"):ResponseDataUtil.buildFail(null);
    }

    /**
     * 获取客户列表
     * @param condition
     * @return
     */
    @PostMapping("/cus")
    private  ResponseData<Page<Customer>> queryCustomers(@RequestBody Condition<Customer> condition){
        Page<Customer> customerPage = customerservice.queryAll(condition);
        return customerPage!=null?ResponseDataUtil.buildOk(customerPage):ResponseDataUtil.buildFail(null);
    }

    /**
     * 获取销售人员列表
     * @param condition
     * @return
     */
    @PostMapping("/sales")
    private  ResponseData<Page<User>> queryUsers(@RequestBody Condition<User> condition){
        Page<User> users = userService.querySales(condition);
        return users!=null?ResponseDataUtil.buildOk(users):ResponseDataUtil.buildFail(null);
    }

    /**
     * 获取商品列表
     * @param condition
     * @return
     */
    @PostMapping("/products")
    private  ResponseData<PageInfo<Inventory>> queryProducts(@RequestBody Condition<Inventory> condition){
        PageInfo<Inventory> products = inventoryToService.selectAll(condition);
        return products!=null?ResponseDataUtil.buildOk(products):ResponseDataUtil.buildFail(null);
    }

    /**
     * 修改订单详情
     * @param order
     * @return
     */
    @PostMapping("/update")
    private ResponseData<Order> updateOrders(@RequestBody Order order){
        Boolean isOK = orderService.updateOrder(order);
        return isOK?ResponseDataUtil.buildOk(204,"修改成功"):ResponseDataUtil.buildFail(null);
    }

    /**
     * 添加订单信息
     * @param order
     * @return
     */
    @PostMapping("/add")
    private ResponseData<Order> addOrders(@RequestBody Order order){
        Boolean isOK = orderService.addOrder(order);
        return isOK?ResponseDataUtil.buildOk(204,"添加成功"):ResponseDataUtil.buildFail(null);
    }

}
